package cn.iocoder.yudao.framework.endpoint;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
import jakarta.annotation.Nonnull;
import jakarta.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.web.context.ServletContextAware;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  <a href="https://blog.csdn.net/nvd11/article/details/136779536">利用 SpringBoot Actuator 来构造 /health /info 等监控接口</a>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/05/24
 */
public class ProjectInfoContributor implements ServletContextAware, InfoContributor {
    private ServletContext servletContext;

    @Value("${spring.profiles.active}")
    private String profile;
    @Value("${xianmin.info.kernel}")
    private String kernel;
    @Value("${xianmin.info.framework}")
    private String framework;
    @Value("${xianmin.info.version}")
    private String application;


    @Override
    public void setServletContext(@Nonnull ServletContext context) {
        this.servletContext = context;
    }

    @Override
    public void contribute(Info.Builder builder) {
        String port = SpringUtils.getProperty("local.server.port", "");
        if (StrUtil.isBlank(port)) {
            port = SpringUtils.getProperty("server.port", "8080");
        }
        builder.withDetail("profile", profile)
                .withDetail("runtime", System.getProperty("java.vm.vendor") + " "
                                       + System.getProperty("java.vm.name") + " "
                                       + System.getProperty("java.vm.version"))
                .withDetail("container", servletContext == null ? "Unknown" : servletContext.getServerInfo())
                .withDetail("kernel", kernel)
                .withDetail("framework", framework)
                .withDetail("application", application)
                .withDetail("port", port);
    }

    public static void main(String[] args) {
        System.out.println("Hello World");
        // 11108@DESKTOP-3PIN60A
        System.out.println("name: " + java.lang.management.ManagementFactory.getRuntimeMXBean().getName());
        // OpenJDK 64-Bit Server VM
        System.out.println("java.vm.name: " + System.getProperty("java.vm.name"));
        // 21.0.2+13-LTS
        System.out.println("java.vm.version: " + System.getProperty("java.vm.version"));
        // Eclipse Adoptium
        System.out.println("java.vm.vendor: " + System.getProperty("java.vm.vendor"));
    }

}
